package com.health.system.action.system;

import static com.health.system.javacommon.util.Struts2JsonHelper.outJson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import com.health.system.javacommon.base.BaseStruts2Action;
import com.health.system.javacommon.util.EncodeDecodeUtil;
import com.health.system.model.system.User;
import com.health.system.service.system.UserManager;
import com.health.system.springSecurity.LogonUser;
import com.health.system.springSecurity.UserInfo;

/**
 * <p>Title: 系统操作员管理 Action</p> 
 * <p>用户基本信息表 TG_USER_INFO</p>
 * <p>Description: 页面通过Ajax调用Action层,返回JSONO数据</p>
 * Copyright: Copyright (c) 2011-03 
 * Company: 
 * 
 * @author zc
 * @version 1.0 
*/

public class UserPasswordAction extends BaseStruts2Action {
	
	private UserManager userManager;
	
	/** 通过spring自动注入 */
	public void setUserManager(UserManager manager) {
		this.userManager = manager;
	}		

	/**
	 * User修改
	 * @throws IOException
	 */
	public void update() throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
			try{
				HttpServletRequest request = getRequest();
				String userJson = request.getParameter("userJson");
				if (null != userJson&&!"".equals(userJson)) {
					JSONObject jsonobject =JSONObject.fromObject( userJson );
					User userobj = (User)JSONObject.toBean(jsonobject, User.class); 
					userobj.setId(jsonobject.get("id").toString());
					userManager.update(userobj);
					result.put("success", true);
					result.put("msg", "修改成功!");
				}else{
					result.put("success", false);
					result.put("msg", "修改失败 数据错误!");
				}
			}catch (Exception e){
				result.put("success", false);
				result.put("msg", "修改失败!");
				e.printStackTrace();
			}
		outJson(result);
	}
	
	/**
	 * 校验密码
	 * throws IOException
	*/
	public void checkPassword() throws IOException{	 
		try{
			HttpServletRequest request = getRequest();
			//从登录用户类中取当前用户信息
			UserInfo userInfo = LogonUser.logonUser();
			//取得Session中用户登陆密码
			String userPassword = userInfo.getPassword();
			//将Session中取得密码解密为原文
			String decodePassword = EncodeDecodeUtil.base64Decode(userPassword);
			//取得页面输入原密码
			String passwordold = request.getParameter("passwordold");
			
			//验证密码输入原密码与登陆密码是否一致
			if(decodePassword.equals(passwordold)){
				getResponse().getWriter().print(true);
			}else{
				getResponse().getWriter().print(false);
			}
			 
		}catch (Exception e){
			getResponse().getWriter().print(false);
			e.printStackTrace();
		}
	}
	/**
	 * 操作员 修改 个人密码
	 * @throws IOException
	 */
	public void personal() throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
			try{
				HttpServletRequest request = getRequest();
				String userJson = request.getParameter("userJson");
				//从Session中取得UserId,cpId
				String userId = LogonUser.getUserId();
				String cpId = LogonUser.getUserCpid();
				//将页面传递参数与UserId拼接起来
				userJson = userJson + ",'id':'" + userId + "','company':'"+ cpId +"'}";
				if (null != userJson&&!"".equals(userJson)) {
					//将页面参数格式化为JSON格式
					JSONObject jsonobject =JSONObject.fromObject( userJson );
					//将JSON数据映射到User实体类上
					User userobj = (User)JSONObject.toBean(jsonobject, User.class); 
					//将UserId,Password(加密),companyId映射到User实体类上
					userobj.setId(jsonobject.get("id").toString());
					userobj.setPassword(EncodeDecodeUtil.base64Encode(jsonobject.get("password").toString()));
					userobj.setCompany(jsonobject.get("company").toString());
					userManager.personal(userobj);
					result.put("success", true);
					result.put("msg", "修改成功!");
				}else{
					result.put("success", false);
					result.put("msg", "修改失败 数据错误!");
				}
			}catch (Exception e){
				result.put("success", false);
				result.put("msg", "修改失败!");
				e.printStackTrace();
			}
		outJson(result);
	}
}
